import requests
import json

if __name__ == "__main__":
    # 1 指定url
    getUrl = "https://movie.douban.com/j/chart/top_list?"
    params = {
        "type": "13",  #
        "interval_id": "100:90",
        "action": "",
        "start": "0",  # 开始位置-从豆瓣服务器的第几部电影开始获取
        "limit": "20"  # 一次请求的取出的电影个数
    }
    # 2 设置UA伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
    }
    # 3 发送get请求
    response = requests.get(url=getUrl, params=params,headers=headers)
    # 4 获取服务器发来的json数据
    listData = response.json()  # 通过抓包工具的响应头内容看数据类型: [] 本次返回一个list列表
    # 5 永久存储数据
    fp = open("./douban.json", "w", encoding="utf-8")
    json.dump(listData, fp=fp, ensure_ascii=False)
    print("**********爬取结束*********")
